import {Component, OnInit} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
import {TrainingService} from "../../../my-service/training.service";
import {Student} from "../../Student";

declare let io: any;

@Component({
  selector: 'app-roster-training-result',
  templateUrl: './roster-training-result.component.html',
  styleUrls: ['./roster-training-result.component.css'],
})
export class RosterTrainingResultComponent implements OnInit {
  trainingID: string;
  trainingName: string;
  students = [];
  className: string;
  socket: any;
  classId: string;

  constructor(private router: Router,
              private routerInfo: ActivatedRoute,
              private trainingService: TrainingService) {
    //创建socket链接
    this.socket = io.connect('http://47.106.117.200:5053');
  }

  getAnswerCardByTrainingId(trainingId) {
    this.trainingService.getAnswerCardByTrainingId(trainingId).then(res => {
      if (res.status == 1) {
        if (res.data[0]) {
          res.data.forEach(card => {
            let student = new Student(card.FK_UserGuid, card.Score, this.classId, this.className, card.PK_AnwserCardGuid);
            student.initInfo(card.UserName, '');
            this.students.push(student);
          })
        }
      }

      if (this.className !== 'null' && this.className) {
        //非匿名情况获取用户信息
        this.students.forEach(student => {
          this.getStuInfo(student);
        });
      }

    })
  }

  /*获取学生信息*/
  getStuInfo(student) {
    if (student.id && student.id !== 'null') {
      //存在用户guid
      this.trainingService.getStuUserInfo(student.id).then(res => {
        if (res.status == 1) {
          let name = res.data.Name ? res.data.Name : res.data.NickName;
          student.initInfo(name, res.data.HeadPhotoPath);
        }
      })
    }

  }

  /*根据训练ID或获取训练信息*/
  getTrainingInfoById(trainingID) {
    this.trainingService.getTrainingByID(trainingID).then(res => {
      if (res.status == 1) {
        this.trainingName = res.data[0].TrainingName;
      }
    })
  }

  ngOnInit() {
    this.trainingID = this.routerInfo.snapshot.params.ID;
    this.className = this.routerInfo.snapshot.params.className;
    this.classId = this.routerInfo.snapshot.params.classID;
    this.getTrainingInfoById(this.trainingID);
    this.getAnswerCardByTrainingId(this.trainingID);
    this.socket.on('grade', (res) => {
      if (this.trainingID == res.trainingID) {
        //只获取该训练提交的答题卡
        let answerCard = res.data[0];
        let falge = false;//判断获取的答题卡是否之前存在
        this.students.forEach(student => {
          if (student.id == answerCard.FK_UserGuid) {
            student.anewScore(answerCard.Score);
            falge = true;
            return;
          }
        });
        if (!falge) {
          //不存在，重新创建学生并且关联答题卡
          let student = new Student(answerCard.FK_UserGuid, answerCard.Score, this.classId, this.className, answerCard.PK_AnwserCardGuid);
          this.students.push(student);
          if (answerCard.FK_UserGuid) {
            //非匿名模式
            this.getStuInfo(this.students[this.students.length - 1]);
          } else {
            //匿名模式
            this.students[this.students.length - 1].initInfo(answerCard.UserName, '')
          }
        }
      }
    });
  }
}
